<!DOCTYPE html>
<html>

<head>
    <title>待办事项</title>
    <style>
        body {
            background-color: #f5f5f5;
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            color: #333;
            background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
            min-height: 100vh;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            position: relative;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        .header h1 {
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            margin: 0;
            color: #333;
            font-size: 24px;
            font-weight: 600;
        }

        .logout-btn {
            padding: 8px 15px;
            background-color: #dc3545;
            color: white;
            border: none;
            border-radius: 4px;
            text-decoration: none;
            position: relative;
            z-index: 1;
            transition: all 0.3s ease;
        }

        .logout-btn:hover {
            background-color: #c82333;
            transform: translateY(-1px);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .filter-form {
            padding: 15px;
            background-color: #f5f5f5;
            border-radius: 4px;
            margin-bottom: 20px;
        }

        .filter-form form {
            display: flex;
            gap: 15px;
            align-items: center;
            flex-wrap: wrap;
        }

        .filter-form input,
        .filter-form select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            min-width: 200px;
            transition: all 0.3s ease;
        }

        .filter-form input:focus,
        .filter-form select:focus {
            border-color: #007bff;
            outline: none;
            box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.1);
        }

        .filter-form button {
            padding: 8px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
            min-width: 80px;
        }

        .filter-form button:hover {
            background-color: #0056b3;
            transform: translateY(-1px);
        }

        .add-form {
            background-color: #fff;
            padding: 20px;
            border-radius: 4px;
            margin: 20px 0;
            border: 1px solid #ddd;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .add-form h2 {
            margin-top: 0;
            color: #333;
            font-size: 1.5em;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }

        .add-form .form-group {
            margin-bottom: 15px;
            position: relative;
        }

        .add-form label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: 500;
        }

        .add-form input,
        .add-form textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            transition: border-color 0.3s ease;
            font-size: 14px;
        }

        .add-form input:focus,
        .add-form textarea:focus {
            border-color: #007bff;
            outline: none;
            box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
        }

        .add-form textarea {
            height: 100px;
            resize: vertical;
            min-height: 60px;
            max-height: 200px;
        }

        .add-form button {
            padding: 10px 20px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: background-color 0.3s ease;
            width: auto;
            min-width: 120px;
        }

        .add-form button:hover {
            background-color: #218838;
            transform: translateY(-1px);
        }

        .add-form button:active {
            transform: translateY(0);
        }

        .todo-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
        }

        .todo-table th,
        .todo-table td {
            padding: 10px;
            border: 1px solid #eee;
            text-align: left;
            vertical-align: top;
        }

        .todo-table th {
            background-color: #f8f9fa;
            font-weight: bold;
            color: #495057;
        }

        .todo-table tr:nth-child(even) {
            background-color: #fafafa;
        }

        .todo-table tr:hover {
            background-color: #f0f0f0;
        }

        .todo-table .actions {
            white-space: nowrap;
            width: 150px;
        }

        .todo-table button {
            margin: 0 5px;
        }

        /* 表格内容样式 */
        .todo-table td span {
            display: block;
            min-height: 20px;
            word-break: break-word;
        }

        .todo-table td:nth-child(2) span {
            white-space: pre-wrap;
            max-height: 100px;
            overflow-y: auto;
        }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 5px;
            margin-top: 30px;
            flex-wrap: wrap;
        }

        .pagination a {
            padding: 5px 10px;
            border: 1px solid #ddd;
            text-decoration: none;
            background-color: white;
            color: #007bff;
            transition: all 0.3s ease;
            min-width: 20px;
            text-align: center;
        }

        .pagination .disabled {
            color: #6c757d;
            pointer-events: none;
            background-color: #f8f9fa;
            border-color: #ddd;
        }

        .pagination-goto {
            margin-left: 15px;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .pagination-goto input {
            width: 50px;
            padding: 4px 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
        }

        .pagination-goto button {
            padding: 4px 8px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .pagination-goto button:hover {
            background-color: #0056b3;
        }

        .pagination a:hover {
            background-color: #f8f9fa;
            border-color: #007bff;
        }

        .pagination a.active {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
            font-weight: bold;
        }

        .empty-state {
            text-align: center;
            padding: 40px 20px;
            color: #6c757d;
            background-color: #f8f9fa;
            border-radius: 4px;
            margin: 20px 0;
        }

        /* 已完成待办事项的样式 */
        .todo-table tr.completed {
            background-color: #e3f2fd !important;
            /* 浅蓝色背景 */
        }

        .todo-table tr.completed td {
            color: #1565c0;
            /* 深蓝色文字 */
        }

        .todo-table tr.completed select {
            background-color: #bbdefb;
            /* 浅蓝色背景 */
            color: #1565c0;
            cursor: not-allowed;
            border-color: #90caf9;
        }

        /* 已完成项目的文本样式 */
        .todo-table tr.completed span {
            color: #1565c0;
            font-weight: 500;
        }

        /* 已完成项目的状态文本 */
        .todo-table tr.completed select option[value="completed"] {
            color: #1565c0;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <div style="width: 85px;"><!-- 占位，与退出按钮宽度相同 --></div>
            <h1>待办事项列表</h1>
            <a href="/logout" class="logout-btn">退出登录</a>
        </div>

        <form action="/todos" method="POST" class="add-form">
            <h2>📝 添加新待办事项</h2>
            <div class="form-group">
                <label>标题：</label>
                <input type="text" name="title" value="" required minlength="1" placeholder="请输入待办事项标题">
            </div>
            <div class="form-group">
                <label>描述：</label>
                <textarea name="description" required minlength="1" placeholder="请输入待办事项详细描述"></textarea>
            </div>
            <button type="submit" onclick="return validateForm()">✨ 添加待办</button>
        </form>

        <div class="filter-form">
            <form action="/todos" method="GET">
                <input type="text" name="search" value="<%= search %>" placeholder="搜索标题或描述">
                <select name="status">
                    <option value="all" <%=status==='all' ? 'selected' : '' %>>全部</option>
                    <option value="pending" <%=status==='pending' ? 'selected' : '' %>>待完成</option>
                    <option value="completed" <%=status==='completed' ? 'selected' : '' %>>已完成</option>
                </select>
                <button type="submit">搜索</button>
            </form>
        </div>

        <h2>待办事项列表</h2>
        <% if (todos.length> 0) { %>
            <table class="todo-table">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>描述</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th class="actions">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <% todos.forEach(function(todo) { %>
                        <tr class="<%= todo.status === 'completed' ? 'completed' : '' %>">
                            <td>
                                <span>
                                    <%= todo.title %>
                                </span>
                            </td>
                            <td>
                                <span>
                                    <%= todo.description || '' %>
                                </span>
                            </td>
                            <td>
                                <form
                                    action="/todos/<%= todo.id %>?page=<%= currentPage %>&status=<%= status %>&search=<%= search %>"
                                    method="POST" style="display: inline;">
                                    <select name="status" onchange="this.form.submit()" <%=todo.status==='completed'
                                        ? 'disabled' : '' %>>
                                        <option value="pending" <%=todo.status==='pending' ? 'selected' : '' %>>待完成
                                        </option>
                                        <option value="completed" <%=todo.status==='completed' ? 'selected' : '' %>>已完成
                                        </option>
                                    </select>
                                </form>
                            </td>
                            <td>
                                <%= new Date(todo.created_at).toLocaleString() %>
                            </td>
                            <td class="actions">
                                <% if (todo.status !=='completed' ) { %>
                                    <form action="/todos/<%= todo.id %>/edit" method="GET" style="display: inline;">
                                        <button type="submit">编辑</button>
                                    </form>
                                    <% } %>
                                        <form action="/todos/<%= todo.id %>/delete" method="POST"
                                            style="display: inline;">
                                            <button type="submit">删除</button>
                                        </form>
                            </td>
                        </tr>
                        <% }); %>
                </tbody>
            </table>

            <div class="pagination">
                <% if (totalPages> 1) { %>
                    <!-- 上一页 -->
                    <a href="/todos?page=<%= Math.max(1, currentPage - 1) %>&status=<%= status %>&search=<%= search %>"
                        class="<%= currentPage === 1 ? 'disabled' : '' %>">
                        上一页
                    </a>

                    <!-- 第一页 -->
                    <% if (currentPage> 3) { %>
                        <a href="/todos?page=1&status=<%= status %>&search=<%= search %>">1</a>
                        <% if (currentPage> 4) { %>
                            <span>...</span>
                            <% } %>
                                <% } %>

                                    <!-- 页码 -->
                                    <% for (let i=Math.max(1, currentPage - 2); i <=Math.min(totalPages, currentPage +
                                        2); i++) { %>
                                        <a href="/todos?page=<%= i %>&status=<%= status %>&search=<%= search %>"
                                            class="<%= currentPage === i ? 'active' : '' %>">
                                            <%= i %>
                                        </a>
                                        <% } %>

                                            <!-- 最后一页 -->
                                            <% if (currentPage < totalPages - 2) { %>
                                                <% if (currentPage < totalPages - 3) { %>
                                                    <span>...</span>
                                                    <% } %>
                                                        <a
                                                            href="/todos?page=<%= totalPages %>&status=<%= status %>&search=<%= search %>">
                                                            <%= totalPages %>
                                                        </a>
                                                        <% } %>

                                                            <!-- 下一页 -->
                                                            <a href="/todos?page=<%= Math.min(totalPages, currentPage + 1) %>&status=<%= status %>&search=<%= search %>"
                                                                class="<%= currentPage === totalPages ? 'disabled' : '' %>">
                                                                下一页
                                                            </a>

                                                            <!-- 跳转到指定页 -->
                                                            <div class="pagination-goto">
                                                                <span>跳转到</span>
                                                                <input type="number" id="pageInput" min="1"
                                                                    max="<%= totalPages %>" value="<%= currentPage %>">
                                                                <button onclick="gotoPage()">确定</button>
                                                            </div>
                                                            <% } %>
            </div>
            <% } else { %>
                <div class="empty-state">
                    <h3>📝 暂无待办事项</h3>
                    <p>快来添加你的第一个待办事项吧！</p>
                </div>
                <% } %>
    </div>

    <script>
        function validateForm() {
            const title = document.querySelector('input[name="title"]').value.trim();
            const description = document.querySelector('textarea[name="description"]').value.trim();

            if (!title || !description) {
                alert('标题和描述都不能为空！');
                return false;
            }
            return true;
        }

        function gotoPage() {
            const pageInput = document.getElementById('pageInput');
            const page = parseInt(pageInput.value);
            const min = parseInt(pageInput.min);
            const max = parseInt(pageInput.max);

            if (page >= min && page <= max) {
                window.location.href = `/todos?page=${page}&status=<%= status %>&search=<%= search %>`;
            } else {
                alert(`请输入有效的页码 (${min}-${max})`);
                pageInput.value = '<%= currentPage %>';
            }
        }

        // 支持回车跳转
        document.getElementById('pageInput').addEventListener('keypress', function (e) {
            if (e.key === 'Enter') {
                gotoPage();
            }
        });
    </script>
</body>

</html>